<!DOCTYPE html>
<html style="height: 100%">

<head>
    <meta charset="utf-8">
    <input id="jsonName" value="baiduToWgs84.json">
    <p style="font-size: 20px;color: red;">输入百度坐标的json文件名导出带有WGS84坐标的csv文件</p>
    <button onclick='tableToExcel()'>导出</button>
</head>

<body style="height: 100%; margin: 0">
    <div id="container" style="height: 100%"></div>
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript" src="gps.js"></script>
    <script type="text/javascript">
        function tableToExcel() {
            $.get('json/' + $("#jsonName").val(), function (data) {
                var coords = [];
                var coordsData = [].concat.apply([], data.map(function (obj, idx) {
                    var coord = {};
                    coord.id = obj.id;
                    coord.bdLat1 = parseFloat(obj.point1_y);
                    coord.bdLng1 = parseFloat(obj.point1_x);
                    coord.bdLat2 = parseFloat(obj.point2_y);
                    coord.bdLng2 = parseFloat(obj.point2_x);
                    coords.push(coord);
                }));
                coords.map(function (obj, idx) {

                    obj.gaodeLat1 = GPS.bd_decrypt(
                        obj.bdLat1, obj.bdLng1).lat;
                    obj.gaodeLng1 = GPS.bd_decrypt(
                        obj.bdLat1, obj.bdLng1).lon;
                    obj.gaodeLat2 = GPS.bd_decrypt(
                        obj.bdLat2, obj.bdLng2).lat;
                    obj.gaodeLng2 = GPS.bd_decrypt(
                        obj.bdLat2, obj.bdLng2).lon;

                    obj.wgsLat1 = GPS.gcj_decrypt_exact(
                        obj.gaodeLat1, obj.gaodeLng1).lat;
                    obj.wgsLng1 = GPS.gcj_decrypt_exact(
                        obj.gaodeLat1, obj.gaodeLng1).lon;
                    obj.wgsLat2 = GPS.gcj_decrypt_exact(
                        obj.gaodeLat2, obj.gaodeLng2).lat;
                    obj.wgsLng2 = GPS.gcj_decrypt_exact(
                        obj.gaodeLat2, obj.gaodeLng2).lon;

                    if (idx == coords.length - 1) {
                        //console.log(gaodeCoordsContact)
                        var jsonData = coords;
                        let str =
                            `id,wgsLat1,wgsLng1,wgsLat2,wgsLng2\n`;
                        //增加\t为了不让表格显示科学计数法或者其他格式
                        for (let i = 0; i < jsonData.length; i++) {
                            for (let item in jsonData[i]) {
                                str += `${jsonData[i][item] + '\t'},`;
                            }
                            str += '\n';
                        }
                        //encodeURIComponent解决中文乱码
                        let uri = 'data:text/csv;charset=utf-8,\ufeff' +
                            encodeURIComponent(str);
                        //通过创建a标签实现
                        var link = document.createElement("a");
                        link.href = uri;
                        //对下载的文件命名
                        link.download = $("#jsonName").val().split('.')[0] +
                            ".csv";
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                    }
                })
            });

        }
    </script>
</body>

</html>